<?php
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 FleaPHP 项目的一部分
//
// Copyright (c) 2005 - 2006 FleaPHP.org (www.fleaphp.org)
//
// 要查看完整的版权信息和许可信息，请查看源代码中附带的 COPYRIGHT 文件，
// 或者访问 http://www.fleaphp.org/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
 * 定义一组便于生成表单元控件的方法
 *
 * @copyright Copyright (c) 2005 - 2006 FleaPHP.org (www.fleaphp.org)
 * @author 廖宇雷 dualface@gmail.com
 * @package Core
 * @version $Id: Html.php 861 2007-06-01 16:37:41Z dualface $
 */
class Helper_HtmlExtjs {
	public $html = array();
	public $attributeList;
	public $Json;
	/**
	 * 生成一个下拉列表框
	 *
	 * @param string $name
	 * @param array $arr
	 * @param mixed $selected
	 * @param string $extra
	 */
	function __construct()
	{
		$this->Json =&new Helper_JSON();
	}
	function html_dropdown_list($name, $arr, $selected = null, $extra = null) {
	    echo "<select name=\"{$name}\" {$extra} >\n";
	    foreach ($arr as $value => $title) {
	        echo '<option value="' . h($value) . '"';
	        if ($selected == $value) { echo ' selected'; }
	        echo '>' . h($title) . "&nbsp;&nbsp;</option>\n";
	    }
	    echo "</select>\n";
	}
	
	/**
	 * 生成一组单选框
	 *
	 * @param string $name
	 * @param array $arr
	 * @param mixed $checked
	 * @param string $separator
	 * @param string $extra
	 */
	function html_radio_group($name, $arr, $checked = null, $separator = '', $extra = null) {
	    $ix = 0;
	    foreach ($arr as $value => $title) {
	        $value_h = h($value);
	        $title = t($title);
	        echo "<input name=\"{$name}\" type=\"radio\" id=\"{$name}_{$ix}\" value=\"{$value_h}\" ";
	        if ($value == $checked) {
	            echo "checked=\"checked\"";
	        }
	        echo " {$extra} />";
	        echo "<label for=\"{$name}_{$ix}\">{$title}</label>";
	        echo $separator;
	        $ix++;
	        echo "\n";
	    }
	}
	
	/**
	 * 生成一组多选框
	 *
	 * @param string $name
	 * @param array $arr
	 * @param array $selected
	 * @param string $separator
	 * @param string $extra
	 */
	function html_checkbox_group($name, $arr, $selected = array(), $separator = '', $extra = null) {
	    $ix = 0;
	    if (!is_array($selected)) {
	        $selected = array($selected);
	    }
	    foreach ($arr as $value => $title) {
	        $value_h = h($value);
	        $title = t($title);
	        echo "<input name=\"{$name}[]\" type=\"checkbox\" id=\"{$name}_{$ix}\" value=\"{$value_h}\" ";
	        if (in_array($value, $selected)) {
	            echo "checked=\"checked\"";
	        }
	        echo " {$extra} />";
	        echo "<label for=\"{$name}_{$ix}\">{$title}</label>";
	        echo $separator;
	        $ix++;
	        echo "\n";
	    }
	}
	
	/**
	 * 生成一个多选框
	 *
	 * @param string $name
	 * @param int $value
	 * @param boolean $checked
	 * @param string $label
	 * @param string $extra
	 */
	function html_checkbox($name, $value = 1, $checked = false, $label = '', $extra = null) {
	    echo "<input name=\"{$name}\" type=\"checkbox\" id=\"{$name}_1\" value=\"{$value}\"";
	    if ($checked) { echo " checked"; }
	    echo " {$extra} />\n";
	    if ($label) {
	        echo "<label for=\"{$name}_1\">" . h($label) . "</label>\n";
	    }
	}
	
	/**
	 * 生成一个文本输入框
	 *
	 * @param string $name
	 * @param string $value
	 * @param int $width
	 * @param int $maxLength
	 * @param string $extra
	 */
	function html_textbox($properity) {
		/*
			{
						fieldLabel: '帐户',
						name: 'name',//元素名称
						//anchor:'95%',//也可用此定义自适应宽度
						allowBlank:false,//不允许为空
						blankText:'帐户不能为空'//错误提示内容
							
			}
		*/
		
		$array = array();
		$array['fieldLabel'] = $properity['fieldLabel'];
		$array['name'] = $properity['name'];
		$array['allowBlank'] = $properity['allowBlank'];
		$array['blankText'] = $properity['blankText'];
		
		$this->html[] = $this->Json->encode($array);
		
	}
	
	/**
	 * 生成一个密码输入框
	 *
	 * @param string $name
	 * @param string $value
	 * @param int $width
	 * @param int $maxLength
	 * @param string $extra
	 */
	function html_password($name, $value = '', $width = null, $maxLength = null, $extra = null) {
	    echo "<input name=\"{$name}\" type=\"password\" value=\"" . h($value) . "\" ";
	    if ($width) {
	        echo "size=\"{$width}\" ";
	    }
	    if ($maxLength) {
	        echo "maxlength=\"{$maxLength}\" ";
	    }
	    echo " {$extra} />\n";
	}
	
	/**
	 * 生成一个多行文本输入框
	 *
	 * @param string $name
	 * @param string $value
	 * @param int $width
	 * @param int $height
	 * @param string $extra
	 */
	function html_textarea($name, $value = '', $width = null, $height = null, $extra = null) {
	    echo "<textarea name=\"{$name}\"";
	    if ($width) { echo "cols=\"{$width}\" "; }
	    if ($height) { echo "rows=\"{$height}\" "; }
	    echo " {$extra} >";
	    echo h($value);
	    echo "</textarea>\n";
	}
	
	/**
	 * 生成一个文件上传域
	 *
	 * @param string $name
	 * @param int $width
	 * @param string $extra
	 */
	function html_filefield($name, $width = null, $extra = null) {
	    echo "<input name=\"{$name}\" type=\"file\"";
	    if ($width) {
	        echo " size=\"{$width}\"";
	    }
	    echo " {$extra} />\n";
	}
	
	/**
	 * 生成 form 标记
	 *
	 * @param string $name
	 * @param string $action
	 * @param string $method
	 * @param string $onsubmit
	 * @param string $extra
	 */
	function html_form($name, $action, $method='post', $onsubmit='', $extra = null) {
	    echo "<form name=\"{$name}\" action=\"{$action}\" method=\"{$method}\" ";
	    if ($onsubmit) {
	        echo "onsubmit=\"{$onsubmit}\"";
	    }
	    echo " {$extra} >\n";
	}
	
	/**
	 * 关闭 form 标记
	 */
	function html_form_close() {
	    echo "</form>\n";
	}
	
	
	/**
	 * 生成一个隐藏域
	 *
	 * @param string $name
	 * @param string $value
	 * @param string $extra
	 */
	function html_hidden($properity) {
		/*
			{
						fieldLabel: '帐户',
						name: 'name',//元素名称
						value: ''
			}
		*/
		$array = array();
		$array['name'] = $properity[name];
		$array['value'] = $properity[value];
		
		$this->html[] = $this->Json->encode($array);
	}
	
	function html_display(){
		foreach ( $element as $key => $value){
			
		}
	
	}
	/*
		$source[name] = 'jack';
		$dest = ['d_name'] = 'name';
		
		return: array('d_name'=>'jack');
	*/
	function html_map($source, $dest){
		
		$ret = array();
		
		foreach ($dest as $d_field => $s_field){
			$ret[$field] = $source[$s_field];	
		}
		
		return $ret;
	}
}

/**
 * 转换 HTML 特殊字符，等同于 htmlspecialchars()
 *
 * @param string $text
 *
 * @return string
 */
function h($text)
{
    return htmlspecialchars($text);
}

/**
 * 转换 HTML 特殊字符以及空格和换行符
 *
 * 空格替换为 &nbsp; ，换行符替换为 <br />。
 *
 * @param string $text
 *
 * @return string
 */
function t($text)
{
    return nl2br(str_replace(' ', '&nbsp;', htmlspecialchars($text)));
}
